package com.hgyc.mom.base.controller;

import java.util.Arrays;
import java.util.List;

import com.hgyc.mom.base.entity.ProductSeries;
import com.hgyc.mom.base.vo.UnitParam;
import jakarta.servlet.http.HttpServletResponse;

import com.hgyc.mom.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hgyc.mom.base.entity.Unit;
import com.hgyc.mom.base.service.UnitService;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;

/**
 * 单位信息Controller
 *
 * @author fwj
 * @since 2025-04-22
 */
@Tag(name = "单位信息控制器", description = "单位信息控制器")
@RestController
@RequestMapping("/base/unit")
public class UnitController
{
    @Autowired
    private UnitService unitService;

    @Operation(summary = "查询单位信息列表", description = "查询单位信息列表")
    @PostMapping("/pageList")
    public Result<IPage<Unit>> pageList(@Parameter(description = "查询参数") @RequestBody UnitParam unitParam)
    {
        Page<Unit> page = new Page<>(unitParam.getPage(), unitParam.getPageSize());
        Unit unit = new Unit();
        BeanUtils.copyProperties(unitParam, unit);
        QueryWrapper<Unit> wrapper = QueryWrapperUtil.build(unit);
        IPage<Unit> list = unitService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "下拉单位列表", description = "下拉单位列表")
    @GetMapping("/selectList")
    public Result<List<Unit>> selectList() {
        return Result.success(unitService.list());
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Unit> util = new ExcelUtil<>(Unit.class);
        util.importTemplateExcel(response, "单位信息" );
    }

    @Operation(summary = "导出单位信息列表", description = "导出单位信息列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, UnitParam unitParam)
    {
        Unit unit = new Unit();
        BeanUtils.copyProperties(unitParam, unit);
        QueryWrapper<Unit> wrapper = QueryWrapperUtil.build(unit);
        List<Unit> list = unitService.list(wrapper);
        ExcelUtil<Unit> util = new ExcelUtil<>(Unit.class);
        util.exportExcel(response, list, "单位信息数据");
    }

    @Operation(summary = "导入单位信息列表", description = "导入单位信息列表")
    @PostMapping("/import")
    public Result importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Unit> util = new ExcelUtil<>(Unit.class);
        List<Unit> unitList = util.importExcel(file.getInputStream());
        String message = unitService.importData(unitList, updateSupport);
        return Result.success(message);
    }

    @Operation(summary = "获取单位信息详细信息", description = "获取单位信息详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "单位信息ID") @PathVariable("id") Long id)
    {
        return Result.success(unitService.getById(id));
    }

    @Operation(summary = "新增单位信息", description = "新增单位信息")
    @PostMapping
    public Result add(@Parameter(description = "单位信息") @RequestBody Unit unit)
    {
        return Result.success(unitService.save(unit));
    }

    @Operation(summary = "修改单位信息", description = "修改单位信息")
    @PutMapping
    public Result update(@Parameter(description = "单位信息") @RequestBody Unit unit)
    {
        return Result.success(unitService.updateById(unit));
    }

    @Operation(summary = "删除单位信息", description = "删除单位信息")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids)
    {
        return Result.success(unitService.removeByIds(Arrays.asList(ids)));
    }

}
